package com.smart.campus.web.controller.attendance;

import java.util.List;

import com.smart.campus.common.utils.DateUtils;
import com.smart.campus.common.utils.StringUtils;
import com.smart.campus.web.utils.SchoolUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.smart.campus.common.annotation.Log;
import com.smart.campus.common.enums.BusinessType;
import com.smart.campus.school.domain.AttendanceRecordValid;
import com.smart.campus.school.service.IAttendanceRecordValidService;
import com.smart.campus.common.core.controller.BaseController;
import com.smart.campus.common.core.domain.AjaxResult;
import com.smart.campus.common.utils.poi.ExcelUtil;
import com.smart.campus.common.core.page.TableDataInfo;

/**
 * 有效打卡数据Controller
 * 
 * @author Zhihe.Li
 * @date 2021-01-03
 */
@Controller
@RequestMapping("/school/attendance/valid")
public class AttendanceRecordValidController extends BaseController
{
    private String prefix = "school/attendance/valid";

    @Autowired
    private IAttendanceRecordValidService attendanceRecordValidService;

    @RequiresPermissions("school:valid:view")
    @GetMapping()
    public String valid()
    {
        return prefix + "/valid";
    }

    /**
     * 查询有效打卡数据列表
     */
    @RequiresPermissions("school:valid:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(AttendanceRecordValid attendanceRecordValid)
    {
        startPage();
        List<AttendanceRecordValid> list = attendanceRecordValidService.selectAttendanceRecordValidList(attendanceRecordValid);
        return getDataTable(list);
    }

    /**
     * 导出有效打卡数据列表
     */
    @RequiresPermissions("school:valid:export")
    @Log(title = "有效打卡数据", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(AttendanceRecordValid attendanceRecordValid)
    {
        List<AttendanceRecordValid> list = attendanceRecordValidService.selectAttendanceRecordValidList(attendanceRecordValid);
        ExcelUtil<AttendanceRecordValid> util = new ExcelUtil<AttendanceRecordValid>(AttendanceRecordValid.class);
        return util.exportExcel(list, "valid");
    }

    /**
     * 新增有效打卡数据
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存有效打卡数据
     */
    @RequiresPermissions("school:valid:add")
    @Log(title = "有效打卡数据", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(AttendanceRecordValid attendanceRecordValid)
    {
        attendanceRecordValid.setSchoolId(SchoolUtil.getSchoolId());
        String l1 = DateUtils.parseDateToStr("yyyyMMdd",attendanceRecordValid.getHpdate());
        if(StringUtils.isNotEmpty(l1)){
            attendanceRecordValid.setYmd(Long.parseLong(l1));
        }
        return toAjax(attendanceRecordValidService.insertAttendanceRecordValid(attendanceRecordValid));
    }

    /**
     * 修改有效打卡数据
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        AttendanceRecordValid attendanceRecordValid = attendanceRecordValidService.selectAttendanceRecordValidById(id);
        mmap.put("attendanceRecordValid", attendanceRecordValid);
        return prefix + "/edit";
    }

    /**
     * 修改保存有效打卡数据
     */
    @RequiresPermissions("school:valid:edit")
    @Log(title = "有效打卡数据", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(AttendanceRecordValid attendanceRecordValid)
    {
        String l1 = DateUtils.parseDateToStr("yyyyMMdd",attendanceRecordValid.getHpdate());
        if(StringUtils.isNotEmpty(l1)){
            attendanceRecordValid.setYmd(Long.parseLong(l1));
        }
        return toAjax(attendanceRecordValidService.updateAttendanceRecordValid(attendanceRecordValid));
    }

    /**
     * 删除有效打卡数据
     */
    @RequiresPermissions("school:valid:remove")
    @Log(title = "有效打卡数据", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(attendanceRecordValidService.deleteAttendanceRecordValidByIds(ids));
    }
}
